package org.vestbjergbyggecenter.Model;

import java.util.ArrayList;
import java.util.Iterator;

public class OrderRecords {
	private ArrayList<Order> orders;
	private static OrderRecords instance = null;
	
	private OrderRecords()
	{
		orders = new ArrayList<>();
	}
	
	public static OrderRecords getInstance()
    {
        if(instance == null)
        {
            instance = new OrderRecords();
        }
        
        return instance;
    }

	public ArrayList<Order> getOrders() {
		return orders;
	}

	public void setOrders(ArrayList<Order> orders) {
		this.orders = orders;
	}	
	
	public void addOrder(Order newOrder)
	{		
		orders.add(newOrder);
	}
	
	public Order getOrder(int id)
	{
		Order theOrder = null;
        boolean found = false;
        Iterator<Order> it = orders.iterator();
        while(it.hasNext() && !found)
        {
            Order order = it.next();
            if(order.getId() == id)
            {
                theOrder = order;
                found = true;
            }
        }        
        return theOrder;		
	}
	
	public void updateOrder(int id, long deliveryDate, String status)
	{
		Order order = getOrder(id);
		order.setOrderingDate(order.getCurrentDate());
		order.setDeliveryDate(deliveryDate);
		order.setStatus(status);
	}
	
	public void removeOrder(int id)
	{
		if(getOrder(id)==null)
		orders.remove(getOrder(id));		
	}
	
	public void print()
	{
		for(Order order:orders)
		{
			order.print();
			System.out.println();
		}
	}
}
